<html>
<head><meta charset="utf-8"><title>when to pass LTO flag · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20pass.20LTO.20flag.html">when to pass LTO flag</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198741973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20pass%20LTO%20flag/near/198741973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20pass.20LTO.20flag.html#198741973">(May 26 2020 at 10:09)</a>:</h4>
<p>Hi, <br>
in gcc/clang lto requires you to pass <code>-flto</code> to both the compiler and linker. <br>
but when I put <code>lto = true</code> in the Cargo.toml and compile with <code>--verbose </code> I see <code>-Clto</code> only in the last rustc invocation and not in all of the deps invocations. how does this work?</p>



<a name="198743018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20pass%20LTO%20flag/near/198743018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20pass.20LTO.20flag.html#198743018">(May 26 2020 at 10:23)</a>:</h4>
<p>When compiling an rlib, the LLVM bitcode is included by default in the resulting rlib in addition to the object files. <code>-Clto</code> is only used to chose between linking the object files and performing LTO using the bitcode.</p>



<a name="198743147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20pass%20LTO%20flag/near/198743147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20pass.20LTO.20flag.html#198743147">(May 26 2020 at 10:25)</a>:</h4>
<p>On nightly you can use <code>-Cembed-bitcode=no</code> to avoid generating LLVM bitcode for rlibs. I believe cargo will use this flag when it lands on stable if lto is disabled to save disk space and a bit of compilation time.</p>



<a name="198743250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20pass%20LTO%20flag/near/198743250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20pass.20LTO.20flag.html#198743250">(May 26 2020 at 10:26)</a>:</h4>
<p><code>-Clinker-plugin-lto</code> disables the generation of object files in the rlibs I believe. I am not sure though.</p>



<a name="198763296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/when%20to%20pass%20LTO%20flag/near/198763296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/when.20to.20pass.20LTO.20flag.html#198763296">(May 26 2020 at 13:44)</a>:</h4>
<p>Just to expand on what bjorn3 said:</p>
<p>Default rlib behavior = both object code and llvm bitcode<br>
<code>-Clinker-plugin-lto</code> = bitcode only (artifact will only be used with LTO)<br>
<code>-Cembed-bitcode=no</code> = object code only (will never be used with LTO)</p>
<p>I think, usually, with a regular rlib, <code>-C lto</code> would just be ignored. Cargo avoids passing it for libraries because the flag is incompatible with other things like <code>-C prefer-dynamic</code>.</p>
<p>This has all recently changed in nightly (1.45), and the details are in <a href="https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/lto.rs">https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/lto.rs</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>